package com.lxp.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.common.CustomException;
import com.lxp.entity.Category;
import com.lxp.entity.Dish;
import com.lxp.entity.Setmeal;
import com.lxp.mapper.CategoryMapper;
import com.lxp.service.CategoryService;
import com.lxp.service.DishService;
import com.lxp.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lxp
 * @Motto 每天一行小代码，健康幸福学JAVA：
 * @create 2022-08-24 10:46
 * @Description:
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    @Override
    public void delete(Long id) {

        //查询当前分类是否关联了菜品，
        // 如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper();
        // 添加查询条件，根据分类id进行查询
        dlqw.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dlqw);
        if (count1 > 0){
            // 已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        // 查询当前分类是否关联了套餐，
        // 如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper();
        // 添加查询条件，根据分类id进行查询
        slqw.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(slqw);
        if (count2 > 0){
            // 已经关联，抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        // 正常删除分类
        super.removeById(id);
    }
}
